//
//  ShowBigImageWithFrameView.m
//  WISDOMCOURT
//
//  Created by ypj on 2019/4/22.
//  Copyright © 2019 ypj. All rights reserved.
//
#define MaxSCale 2.5  //最大缩放比例
#define MinScale 0.5  //最小缩放比例
#import "ShowBigImageWithFrameView.h"

@implementation ShowBigImageWithFrameView
-(instancetype)initWithFrame:(CGRect)frame  withImage:(UIImage  *)image
{
    self=[super initWithFrame:frame];
    if (self) {
        self.totalScale = 1.0;
        self.backgroundColor= [UIColor blackColor];
        self.userInteractionEnabled=YES;
//        NSLog(@"-------%f%f",image.size.width,image.size.height);
        //点击手势
        [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(disMissView)]];
        // CGFloat  imageHeight=self.width*(1334/750.0);
        _bigImage=[[UIImageView alloc] initWithFrame:CGRectMake(0, Height_NavBar , ScreenWidth,520)];
        _bigImage.center = self.center;
        _bigImage.image=image;
        _bigImage.contentMode = UIViewContentModeScaleAspectFit;
        [self addSubview:_bigImage];
        //放大缩小手势
        _bigImage.userInteractionEnabled=YES;
        UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
        [_bigImage addGestureRecognizer:pinch];
        
//          //图片拖拉手势
        UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(changePoint:)];
        [_bigImage addGestureRecognizer:panGesture];
        
        
    }
    return self;
}
#pragma mark----放大缩小手势、拖动手势、点击手势
- (void)pinch:(UIPinchGestureRecognizer *)recognizer{
    
    CGFloat scale = recognizer.scale;
    
    //放大情况
    if(scale > 1.0){
        if(self.totalScale > MaxSCale) return;
    }
    
    //缩小情况
    if (scale < 1.0) {
        if (self.totalScale < MinScale) return;
    }
    
    _bigImage.transform = CGAffineTransformScale(_bigImage.transform, scale, scale);
    self.totalScale *=scale;
    recognizer.scale = 1.0;
    
}
- (void)changePoint:(UIPanGestureRecognizer *)sender {
       UIView *view = sender.view;
       if (sender.state == UIGestureRecognizerStateBegan || sender.state == UIGestureRecognizerStateChanged) {
             CGPoint translation = [sender translationInView:view.superview];
        
           [view setCenter:CGPointMake(view.centerX+translation.x, view.centerY+translation.y)];
           
               [sender setTranslation:CGPointZero inView:view.superview];
          
       }
    }
-(void)disMissView{
    
     [self removeFromSuperview];
}
@end
